package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.InterestImpl;
import com.shisfish.library.Model.Interest;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("interest")
public class InterestController extends GlobalVariable {

	private InterestImpl interestImpl = new InterestImpl();
	
	@POST
	@Path("insertAInterest/{idadmin}")
	@Produces("application/json; charset=utf-8")
	public String insertAInterest(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("isMust") int isMust, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			Interest interest = new Interest(name, isMust);
			int flag = interestImpl.insertOne(interest);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, INSERT_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAInterset/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateAInterset(@PathParam("idadmin") String idadmin, @FormParam("name") String name, 
			@FormParam("isMust") int isMust, @Context HttpServletRequest request, 
			@PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			List<Interest> interests = interestImpl.selectById(id);
			if (interests.size() > 0) {
				Interest interest = interests.get(0);
				interest.setIsMust(isMust);
				interest.setName(name);
				int flag = interestImpl.updateOne(interest);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("deleteAInterest/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String deleteAInterest(@PathParam("idadmin") String idadmin, 
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			int flag = interestImpl.deleteOneById(id);
			if (flag == 0) {
				globalMap.put(STATUS_CODE, DELETE_WR);
			} else {
				globalMap.put(STATUS_CODE, SUCCESS);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAll")
	@Produces("application/json; charset=utf-8")
	public String selectAll() {
		Map<String, Object> globalMap = new HashMap<String, Object>();
	
		List<Interest> interests = interestImpl.selectAll();
		if (interests.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("list", interests);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
	
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAllPage/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("currentPage") int currentPage, @PathParam("pageSize") int pageSize) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
	
		int count = interestImpl.countAll();
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			List<Interest> interests = interestImpl.selectAllPage(currentPage, pageSize);
			if (interests.size() > 0) {
				globalMap.put(STATUS_CODE, SUCCESS);
				globalMap.put("page", page);
				globalMap.put("list", interests);
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectType/{type}")
	@Produces("application/json; charset=utf-8")
	public String selectType(@PathParam("type") int type) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
	
		List<Interest> interests = interestImpl.selectType(type);
		if (interests.size() > 0) {
			globalMap.put(STATUS_CODE, SUCCESS);
			globalMap.put("list", interests);
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
	
		return toJsonUtils.map2Json(globalMap);
	}
	
}
